package com.shundr.auth.common.view;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.shundr.auth.channel.R;
import com.shundr.truck.model.TruckInfo;

public class DialogChoiceTruckNum extends Dialog {
	private Context mContext;
	private List<TruckInfo> mList = new ArrayList<TruckInfo>();
	private ListView lvTruckList;
	private TruckInfoAdapter mTruckInfoAdapter;
	private OnChoiceTruckListener OnChoiceTruckListener;

	public DialogChoiceTruckNum(Context context, List<TruckInfo> list) {
		super(context, R.style.dialog_style);
		this.mContext = context;
		this.mList = list;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_dialog_choice_truck);
		setCanceledOnTouchOutside(true);
		lvTruckList = (ListView) findViewById(R.id.lv_truck_list);
		mTruckInfoAdapter = new TruckInfoAdapter();
		lvTruckList.setAdapter(mTruckInfoAdapter);
		lvTruckList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				OnChoiceTruckListener.getTruckInfo(position);
				dismiss();
			}
		});
	}

	class TruckInfoAdapter extends BaseAdapter {

		public TruckInfoAdapter() {
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int index) {
			return mList.get(index);
		}

		@Override
		public long getItemId(int id) {
			return id;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup viewGroup) {
			ViewHold viewHold = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.item_truck_number_item, null);
				viewHold = new ViewHold();
				viewHold.truck = (TextView) convertView
						.findViewById(R.id.tv_truck_text);
				convertView.setTag(viewHold);
			} else {
				viewHold = (ViewHold) convertView.getTag();
			}
			try {
				viewHold.truck.setText(mList.get(position)
						.getTruckPlateNumber());
			} catch (Exception e) {
				e.printStackTrace();
			}
			return convertView;
		}

		class ViewHold {
			public TextView truck;
		}
	}

	public interface OnChoiceTruckListener {
		public void getTruckInfo(int position);
	}

	public OnChoiceTruckListener getOnChoiceTruckListener() {
		return OnChoiceTruckListener;
	}

	public void setOnChoiceTruckListener(
			OnChoiceTruckListener onChoiceTruckListener) {
		OnChoiceTruckListener = onChoiceTruckListener;
	}

}
